import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:workmate/models/config.dart';
import 'package:workmate/provider/global.dart';
import 'package:workmate/provider/theme.dart';
import 'package:workmate/routers/pages.dart';
import 'package:workmate/services/index.dart';
import 'dart:math';

/// 启动页、闪屏广告页、引导页
class SplashPage extends StatefulWidget {
  const SplashPage({Key? key}) : super(key: key);

  @override
  SplashPageState createState() {
    return SplashPageState();
  }
}

typedef ContextCallback = void Function(BuildContext context);

class SplashPageState extends State<SplashPage> {
  // 皮肤切换
  bool switchThemeFlag = true;
  bool switchFontFlag = true;
  int lightNumber = 0;
  int lightNumberStep = 50;
  bool blendModeFlag = true;
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      initRequest();
    });
  }

  initRequest() async {
    debugPrint('initRequest');
    // 获取配置并设置配置
    Map<String, dynamic> api = await CommonService.getConfig();
    APP.config = ConfigModel.fromJson({});
    ConfigModel.apiFromJson(api);
    await Future.delayed(const Duration(milliseconds: 3000));
    // 跳转到页面，支持传参数，参数支持数组
    // APP.router.goto('/home', query: {
    //   'params': ['params1', 'params2', 'params2'],
    //   'user': 'userParams',
    // });
  }

  @override
  Widget build(BuildContext context) {
    ThemeProvider theme = Provider.of<ThemeProvider>(context);
    return Scaffold(
        body: Stack(alignment: Alignment.bottomCenter, children: <Widget>[
      Container(
        padding: const EdgeInsets.all(20),
        color: theme.primaryColor,
        // color: Colors.deepOrange[200],
        child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text('splash.title',
                      style: TextStyle(color: Colors.white, fontSize: 48))
                  .tr(),
              Padding(
                padding: const EdgeInsets.only(bottom: 250),
                child: const Text('splash.desc',
                        style: TextStyle(color: Colors.white, fontSize: 16))
                    .tr(),
              ),
            ]),
      ),
      Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: Container(
              padding: const EdgeInsets.fromLTRB(0, 10, 0, 30),
              color: const Color.fromRGBO(255, 255, 255, 0.3),
              child: Center(
                  child: Column(children: [
                Wrap(children: [
                  _navNutton('btns.gohome', route: Pages.home, params: {
                    'params': ['params1', 'params2', 'params3'],
                    'user': 'userParams',
                  }),
                  _navNutton(tr('btns.language'), context: context,
                      callback: () {
                    String ls = context.locale.toString();
                    Locale l;
                    switch (ls) {
                      case 'en':
                        l = const Locale('zh', 'CN');
                        break;
                      case 'zh_CN':
                        l = const Locale('en');
                        break;
                      default:
                        l = const Locale('zh', 'CN');
                    }
                    debugPrint(l.toString());
                    context.setLocale(l);
                  }),
                  _navNutton(tr('btns.theme'), callback: () {
                    theme.switchTheme(
                        color: switchThemeFlag
                            ? Colors.green
                            : theme.initPrimaryColor);
                    switchThemeFlag = !switchThemeFlag;
                  }),
                  _navNutton(tr('btns.fonts'), callback: () {
                    theme.switchFont(switchFontFlag ? 1 : 0);
                    switchFontFlag = !switchFontFlag;
                  }),
                  _navNutton(tr('btns.light'), callback: () {
                    lightNumber = lightNumber + lightNumberStep;
                    if (lightNumber >= 200) {
                      lightNumber = min(lightNumber, 200);
                      lightNumberStep *= -1;
                    }
                    if (lightNumber <= 0) {
                      lightNumber = max(lightNumber, 0);
                      lightNumberStep *= -1;
                    }
                    theme.setLightNumber(lightNumber);
                  }),
                  _navNutton(tr('btns.gray'), callback: () {
                    theme.switchBlendMode(
                        blendModeFlag ? BlendMode.darken : BlendMode.color);

                    blendModeFlag = !blendModeFlag;
                  })
                ]),
                SizedBox(
                  height: 20,
                  child: const Text('logo',
                          style: TextStyle(
                              fontSize: 14,
                              height: 20 / 14,
                              color: Colors.white))
                      .tr(),
                )
              ]))))
    ]));
  }

  _navNutton(String title,
      {BuildContext? context,
      String? route,
      Map<String, dynamic>? params,
      Function? callback}) {
    return TextButton(
      style: ButtonStyle(
        padding: MaterialStateProperty.all(const EdgeInsets.all(0)),
      ),
      onPressed: () {
        debugPrint(MediaQuery.of(context ?? APP.navigatorKey.currentContext!)
            .size
            .toString());
        if (callback != null) {
          callback();
          return;
        }
        APP.router.goto(route!, query: params!, replace: true);
      },
      child: Text(
        title,
        textAlign: TextAlign.center,
        style: const TextStyle(fontSize: 14),
      ).tr(),
    );
  }
}
